package com.garlic.face.jlf.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;

/**
 * @author wenhui02.liu
 * @date 2018/8/8.
 */

public class BaseActivity extends Activity {
    private Dialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        App.addActivity(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.hideProgress();
        App.removeActivity(this);
    }

    public void hideProgress() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public void showProgress() {
        hideProgress();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View root = View.inflate(BaseActivity.this, R.layout.garlic_general_loading_dialog, null);
                ImageView spaceshipImage = (ImageView) root.findViewById(R.id.img);
                TextView mProgressDialogMessage = (TextView) root.findViewById(R.id.tipTextView);// 提示文字
                Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                        BaseActivity.this, R.anim.garlic_general_progress_rotate);
                spaceshipImage.startAnimation(hyperspaceJumpAnimation);
                mProgressDialogMessage.setText(R.string.please_wait);

                AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this).setView(root)
                        .setCancelable(false);
                mProgressDialog = builder.create();
                mProgressDialog.setCanceledOnTouchOutside(false);
                mProgressDialog.show();
            }
        });

    }

    public void showProgress(String content){
        hideProgress();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View root = View.inflate(BaseActivity.this, R.layout.garlic_general_loading_dialog, null);
                ImageView spaceshipImage = (ImageView) root.findViewById(R.id.img);
                TextView mProgressDialogMessage = (TextView) root.findViewById(R.id.tipTextView);// 提示文字
                Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                        BaseActivity.this, R.anim.garlic_general_progress_rotate);
                spaceshipImage.startAnimation(hyperspaceJumpAnimation);
                mProgressDialogMessage.setText(content);

                AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this).setView(root)
                        .setCancelable(false);
                mProgressDialog = builder.create();
                mProgressDialog.setCanceledOnTouchOutside(false);
                mProgressDialog.show();
            }
        });
    }

}
